package controller.main
{
	import events.main.EventBrowseForFiles;
	import events.main.EventLoadProject;
	
	import flash.filesystem.File;
	import flash.utils.ByteArray;
	
	import model.ModelBrowseFile;
	import model.ModelLibrary;
	import model.ModelProject;
	import model.ModelRooms;
	
	import mx.controls.Alert;
	
	import org.flashIso.engine.base.IsoRoom;
	import org.flashIso.engine.library.objects.LBinaryData;
	import org.flashIso.engine.library.objects.LImage;
	import org.flashIso.engine.library.objects.LObject;
	import org.flashIso.engine.library.objects.LTile;
	import org.robotlegs.mvcs.Command;
	
	import others.EditorIsoFactory;
	
	import services.IServiceFile;
	
	public class CommandLoadProject extends Command
	{
		[Inject]
		public var mp:ModelProject;
		
		[Inject]
		public var mb:ModelBrowseFile;
		
		[Inject]
		public var ml:ModelLibrary;
		
		[Inject]
		public var mr:ModelRooms;
		
		[Inject]
		public var fs:IServiceFile;
		
		[Inject]
		public var e:EventLoadProject;
		
		
		override public function execute() : void {
			if (e.path == null || e.path.length == 0){
				dispatch(new EventBrowseForFiles(EventBrowseForFiles.BROWSE_FOR_FILES, mb.BROWSE_FOR_LOAD_PROJECT));
			}else{
				
//				try{
					var i:uint;
					
					//load images
					ml.imagesDataProvider.removeAll();
					var imagesDir:File = new File(e.path).parent.resolvePath("images");
					if (imagesDir.exists){
						var files:Array = imagesDir.getDirectoryListing();
						for (i = 0; i < files.length; i++){
							var img:LImage = new LImage();
							img.id = (files[i] as File).name;
							img.data = fs.readFile((files[i] as File).nativePath);
							ml.imagesDataProvider.addItem(img);
						}
					}
					
					
					//load binaries
					ml.binaryDataProvider.removeAll();
					var binariesDir:File = new File(e.path).parent.resolvePath("binaries");
					if (binariesDir.exists){
						files = binariesDir.getDirectoryListing();
						for (i = 0; i < files.length; i++){
							var binary:LBinaryData = new LBinaryData();
							binary.id = (files[i] as File).name;
							binary.data = fs.readFile((files[i] as File).nativePath);
							ml.binaryDataProvider.addItem(binary);
						}
					}
					
					//load xml
					ml.objectsDataProvider.removeAll();
					var xml:XML = new XML(fs.readFile(e.path).toString());
					var objectsXML:XML = xml.child("Library")[0].child("Objects")[0];
					for (i = 0; i < objectsXML.children().length(); i++){
						var lObject:LObject = new LObject();
						lObject.fromXML(objectsXML.children()[i], ml);
						ml.addLObject(lObject);
					}
				
					ml.tilesDataProvider.removeAll();
					var tilesXML:XML = xml.child("Library")[0].child("Tiles")[0];
					for (i = 0; i < tilesXML.children().length(); i++){
						var lTile:LTile = new LTile();
						lTile.fromXML(tilesXML.children()[i], ml);
						ml.addLTile(lTile);
					}
					
					mr.roomsDataProvider.removeAll();
					var roomsXML:XML = xml.child("Rooms")[0];
					for (i = 0; i < roomsXML.children().length(); i++){
						var room:IsoRoom = new IsoRoom();
						room.fromXML(roomsXML.children()[i], ml);
						mr.addRoom(room);
					}
					
					mp.projectFilePath = e.path;
					
//				}catch(e:Error){
//					Alert.show(e.message, "Unable to load project!");
//				}
				

			}
		}
	}
}